import Vue from 'vue'
import Router from 'vue-router'

import Login from '../pages/Login.vue'
import Home from '../pages/Home.vue'
import Wellcome from '../pages/Wellcome.vue'
import Users from '../pages/user/Users.vue'
import Rights from '../pages/power/Rights.vue'

import Roles from '../pages/power/Roles.vue'
import Cate from '../pages/goods/Cate.vue'
import Params from '../pages/goods/Params.vue'
import Lists from '../pages/goods/Lists.vue'
import Add from '../pages/goods/Add.vue'
import Order from '../pages/order/Order.vue'


Vue.use(Router)

const router =  new Router({
    routes:[
        {
            path:'/',
            redirect:'/login'
        },
        {
            name:'login',
            path:'/login',
            component:Login
        },
        {
            name:'home',
            path:'/home',
            component:Home,
            redirect:'/wellcome',
            children:[
                {name:'wellcome',path:'/wellcome',component:Wellcome},
                {name:'users',path:'/users',component:Users},
                {name:'rights',path:'/rights',component:Rights},
                {name:'roles',path:'/roles',component:Roles},
                {name:'cate',path:'/categories',component:Cate},
                {name:'params',path:'/params',component:Params},
                {name:'lists',path:'/goods',component:Lists},
                {name:'Add',path:'/goods/add',component:Add},
                {name:'Order',path:'/orders',component:Order},
            ]
        },
    ]
})

// 路由守卫
router.beforeEach((to,from,next)=>{
    // console.log(to,from);
    if(to.name === 'login') return next();
    const tokenStr = sessionStorage.getItem('token')
    if(!tokenStr) {
        return next('/login') 
    }
    next();
})

export default router